package com.example.helloword;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.helloword.bean.Book;
import com.example.helloword.bean.Chapter;
import com.example.helloword.bean.User;
import com.example.helloword.databinding.ActivityTextBinding;
import com.example.helloword.source.BaseNovelSource;
import com.example.helloword.source.BiqupaiNoveSource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class TextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTextBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_text);
        User user = new User();
        viewDataBinding.setUser(user);
        viewDataBinding.but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setCont();
            }
        });
//        setContentView(R.layout.activity_text);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);

//        TextView sb =  findViewById(R.id.read_tv);
//        sb.setMovementMethod(ScrollingMovementMethod.getInstance());
//        sb.setSelected(true);
//        sb.setText(load());
        crawl();

    }


    public String load() {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content;
        content = new StringBuilder();

        try {
//            in= (FileInputStream) getAssets().open("q37882");
            in = new FileInputStream(new File(Environment.getExternalStorageDirectory(), "37882.txt"));
//            in=new FileInputStream(new File(Environment.getExternalStorageDirectory()+ "/37882.txt"));
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                content.append("\n");
                content.append(line);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return content.toString();

    }

    public void crawl() {
//        String url = "https://www.biqupai.com/140_140222/";
        String url = "https://www.biqupai.com/search.php?keyword=一";
                RequestParams params = new RequestParams(url);
        BaseNovelSource baseNovelSource = new BiqupaiNoveSource();

//        RequestParams params = new RequestParams(baseNovelSource.getSearchUrl("一"));
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {

//
//////                updateDB(city, result, "tdbean");
//                Log.w("crawl","Td请求成功");
////                Log.w("crawl",result);
//
//                Document parse = Jsoup.parse(result);
////                Elements title = parse.select("div.result-list.gameblock-result-list");
//                Elements title = parse.select("div.result-item.result-game-item");
//
//                for (Element title1:title) {
//                    Log.w("crawl1",title1.toString());
//
//
//                }
//

                List<Book> items = baseNovelSource.getItems(result, "div.result-item.result-game-item",
                   BaseNovelSource.BOOK_LIST
//
//                );
//                List<Chapter> items = baseNovelSource.getItems(result, "dd a",
//                   BaseNovelSource.CHAPTER_LIST

                );

//                Log.w("crawl1", items.toString());


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("crawl", "Td请求失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }


}